package cn.dengta.webapp.user.form;

import cn.dengta.common.util.Common;
import cn.dengta.common.validator.Check;
import cn.dengta.common.validator.Range;
import cn.dengta.webapp.base.form.BaseForm;
import cn.dengta.webapp.user.util.spatial.Coords;
import lombok.*;
import me.codeplayer.util.*;

@Getter
@Setter
public class CoordsForm extends BaseForm<Coords> {

	/** 地理经度位置（坐标） */
	@Check(value = "longitude", required = false)
	@Range(min = 0, max = 180)
	public Double lon;
	@Check(value = "latitude", required = false)
	@Range(min = 0, max = 90)
	public Double lat;
	@Setter(AccessLevel.NONE)
	public String realCoords;
	@Setter(AccessLevel.NONE)
	public transient Coords coords;

	@Override
	public void preHandle() {
		super.preHandle();
		Assert.isTrue(X.matchAllOrNone(lon == null, lat == null));
		if (lon != null) {
			coords = new Coords(lon, lat);
			realCoords = lon + Common.SEP + lat;
		}
	}

	public void setLon(Double lon) {
		this.lon = lon;
	}

	public void setLat(Double lat) {
		this.lat = lat;
	}

	public void setLon(String lon) {
		this.lon = NumberUtil.getDouble(lon);
	}

	public void setLat(String lat) {
		this.lat = NumberUtil.getDouble(lat);
	}

}
